package com.neuedu.web.servlet.user; /**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-18 11:46
 * 描述 : ${dc}
 */

import com.neuedu.web.dao.UserDao;
import com.neuedu.web.entity.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * http://localhost:8080/web/userlist
 *
 */
@WebServlet(name = "UserListServlet", value = "/userlist")
public class UserListServlet extends HttpServlet {
    UserDao userDao = new UserDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取响应对象 out
        //设置响应的类型
        response.setContentType("application/object"); //文本---html
        // response.setContentType("text/plain"); //简单文本
        response.setContentType("text/html;charset=utf-8"); //文本---html


        //在响应内容之前，设置编码格式 contenttype
        response.setCharacterEncoding("utf-8");




        // response.getOutputStream()
        PrintWriter out = response.getWriter();



        try {
            UserDao userDao = new UserDao();
            String sql = " select * from user";
            Object[] params = {};
            List<User> users = userDao.selectList(sql, params);

            StringBuilder html = new StringBuilder();
            html.append( "  <a href='userAdd'>添加</a> 							");
            html.append( "  <table width='100%' border='1' cellpadding='0' cellspacing='0' >							");
            html.append( "  	<tr>                      "  );
            html.append( "  		<td>序号</td>         "  );
            html.append( "  		<td>用户名</td>       "  );
            html.append( "  		<td>密码</td>         "  );
            html.append( "  		<td>昵称</td>         "  );
            html.append( "  		<td>出生日期</td>     "  );
            html.append( "  		<td>email</td>        "  );
            html.append( "  		<td>操作</td>        "  );
            html.append( "  	</tr>                     "  );
            for (int i = 0; i < users.size(); i++) {
                User user = users.get(i);
                html.append( "  	<tr>                      "  );
                html.append( "  		<td>"+i+"</td>            "  );
                html.append( "  		<td>"+user.getUsername()+"</td>        "  );
                html.append( "  		<td>"+user.getPassword()+"</td>        "  );
                html.append( "  		<td>"+user.getDisplayname()+"</td>        "  );
                html.append( "  		<td>"+user.getBirthday()+"</td>        "  );
                html.append( "  		<td>"+user.getEmail()+"</td>        "  );
                html.append( "  		<td><a href='userDelete?id="+user.getId()+"' >删除</a> &nbsp;&nbsp;  <a href='userEdit?id="+user.getId()+"' >更新</a></td>        "  );

            }
            html.append( "                                "  );
            html.append( "  	</table>                  "  );

            // out.输出 html
            out.write(html.toString());

            out.flush();
            out.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
